package com.kla.power.controller;


import com.kla.power.config.LogPointer;
import com.kla.power.entity.ScreenUserEntity;
import com.kla.power.entity.UserEntity;
import com.kla.power.service.IScreenUserService;
import com.kla.power.util.RedisUtils;
import com.kla.power.util.ResultBody;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 大屏用户关联，每位用户可独立设置大屏标题 前端控制器
 * </p>
 *
 * @author WuZH
 * @since 2024-08-06
 */
@RestController
@RequestMapping ("/screenUser")
@Slf4j
@CrossOrigin (value = "*", maxAge = 3600)
public class ScreenUserController {


    @Resource
    private IScreenUserService iScreenUserService;

    @RequestMapping ("queryList")
    @LogPointer ("查询商户下大屏列表")
    public ResultBody queryList ( @RequestBody ScreenUserEntity screenUserEntity) {
        UserEntity userEntity = (UserEntity) RedisUtils.get ( screenUserEntity.getToken () );
        screenUserEntity.setUserId ( userEntity.getId () );
        List<ScreenUserEntity> screenMerchantEntities = iScreenUserService.queryList ( screenUserEntity );
        return ResultBody.success ( screenMerchantEntities );
    }

    @RequestMapping ("saveSingle")
    @LogPointer ("保存商户下大屏")
    public ResultBody saveSingle (@RequestBody ScreenUserEntity screenUserEntity) {
        UserEntity userEntity = (UserEntity) RedisUtils.get ( screenUserEntity.getToken () );
        screenUserEntity.setUserId ( userEntity.getId () );
        screenUserEntity = iScreenUserService.saveSingle ( screenUserEntity );
        return ResultBody.success ( screenUserEntity );
    }

    @RequestMapping ("updateSingle")
    @LogPointer ("更新商户大屏")
    public ResultBody updateSingle (@RequestBody ScreenUserEntity screenUserEntity) {
        UserEntity userEntity = (UserEntity) RedisUtils.get ( screenUserEntity.getToken () );
        screenUserEntity.setUserId ( userEntity.getId () );
        screenUserEntity = iScreenUserService.updateSingle ( screenUserEntity );
        return ResultBody.success ( screenUserEntity );
    }

}
